#include <Arduino.h>
#include <Wire.h>
#include "at24cxx.h"

void at24cxx_init(void)
{
    Wire.begin();
}

static void at24cxx_wait(int slave)
{
    int result = 0;
    int retries = 10; // 设置最大重试次数
    do
    {
        Wire.beginTransmission(slave);
        result = Wire.endTransmission();
        Serial.printf("[at24cxx_wait] 0x%02x return %d\n", slave, result);
        
        if (result != 0)
        {
            retries--; // 减少重试次数
            if (retries == 0)
            {
                Serial.printf("[at24cxx_wait] 0x%02x return %d\n[at24cxx_wait]err to more,break\n");
                break; // 达到最大重试次数，退出循环
            }
        }

        delay(10); // 添加短暂的延迟
    } while (result != 0);
}


void at24cxx_write_byte(int slave, int addr, int data)
{
    at24cxx_wait(slave);
    Wire.beginTransmission(slave);
    Wire.write(addr);  /*发送写地址*/
    Wire.write(data);  /*写数据*/
    int resault = Wire.endTransmission();
    Serial.printf("[at24cxx_write_byte]0x%02x data[0x%02x] return%d\n",slave,data,resault);
}

int at24cxx_read_byte(int slave, int addr)
{
    int  ret = 0xFF;
    at24cxx_wait(slave);
    Wire.beginTransmission(slave);
    Wire.write(addr);  /*发送读地址*/
    int resault = Wire.endTransmission();   
    /*读数据*/
    Wire.requestFrom(slave, 1);
    ret = Wire.read();
    Serial.printf("[at24cxx_read_byte]0x%02x data[0x%02x] return%d\n",slave,ret,resault);
    return ret;
}

int at24cxx_write(int slave, int addr, const uint8_t *buf, int buflen)
{
    int left_len = buflen;
    int send_len = PAGE_SIZE-(addr%PAGE_SIZE);
    send_len = (left_len>send_len) ? send_len:left_len;
    while(left_len)
    {
        at24cxx_wait(slave);
        Wire.beginTransmission(slave);
        Wire.write(addr);  /*发送写地址*/
        Wire.write(buf, send_len);  /*写数据*/
        int resault = Wire.endTransmission();
        Serial.printf("[at24cxx_write]0x%02x data[0x%02x] return%d\n",slave,buf,resault);
        left_len -= send_len;
        addr += send_len;
        buf += send_len;
        send_len = (left_len>PAGE_SIZE) ? PAGE_SIZE:left_len;
    }
    return buflen;
}


int at24cxx_read(int slave, int addr, uint8_t *buf, int buflen)
{
    int left_len = buflen;
    int get_len = 0;
    while(left_len)
    {
        if(left_len > 32)
        {
            get_len = 32;
        }
        else
        {
            get_len = left_len;
        }
        at24cxx_wait(slave);
        Wire.beginTransmission(slave);
        Wire.write(addr);  /*发送读地址*/
        int resault = Wire.endTransmission();
        Serial.printf("[at24cxx_read]0x%02x data[0x%02x] return%d\n",slave,addr,resault);
        /*读数据*/
        Wire.requestFrom(slave, get_len);
        int add = addr;
        for(int i=0; i<get_len; i++)
        {
            buf[i] = Wire.read();
            Serial.printf("[at24cxx_read]0x%02x addr[0x%02x]data[0x%02x]\n",slave,add,buf[i]);
            add++;
        }
        left_len -= get_len;
        buf += get_len;
        addr += get_len;
    }
    return buflen;
}